package com.hu.auth.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hu.auth.common.aop.Verify;
import com.hu.auth.common.result.R;
import com.hu.auth.model.Role;
import com.hu.auth.model.po.AssignMenuPo;
import com.hu.auth.model.po.RolePo;
import com.hu.auth.service.MenuService;
import com.hu.auth.service.RoleService;
import io.swagger.annotations.Api;
import org.springframework.beans.BeanUtils;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Api(tags = "角色模块")
@RestController
@RequestMapping("/role")
public class RoleController {

    @Resource
    RoleService roleService;
    @Resource
    MenuService menuService;

    @Verify(key = "bnt.sysRole.list")
    @GetMapping("/list/{page}/{limit}")
    public R list(@PathVariable int page, @PathVariable int limit, String name) {
        if (StringUtils.isEmpty(name)) {
            return R.ok(roleService.page(new Page<>(page, limit)));
        } else {
            return R.ok(roleService.page(new Page<>(page, limit),
                    new QueryWrapper<Role>().lambda().like(Role::getRoleName, name)));
        }
    }

    @Verify(key = "bnt.sysRole.list")
    @GetMapping("/getById/{id}")
    public R getById(@PathVariable Long id) {
        if (id < 1) {
            return R.fail().message("参数错误");
        }
        return R.ok(roleService.getById(id));
    }

    @Verify(key = "bnt.sysRole.add")
    @PostMapping("/add")
    public R add(@RequestBody @Validated RolePo po) {
        if (po.getId() != null) {
            return R.fail().message("参数错误");
        }
        if (roleService.count(new QueryWrapper<Role>().lambda().eq(Role::getRoleName, po.getRoleName())) > 0) {
            return R.fail().message("角色名出现重复");
        }
        roleService.add(po);
        return R.ok();
    }

    @Verify(key = "bnt.sysRole.update")
    @PostMapping("/update")
    public R update(@RequestBody @Validated RolePo po) {
        if (po.getId() == null) {
            return R.fail().message("参数错误");
        }
        if (roleService.count(new QueryWrapper<Role>().lambda()
                .ne(Role::getId, po.getId())
                .eq(Role::getRoleName, po.getRoleName())) > 0) {
            return R.fail().message("角色名出现重复");
        }
        Role role = new Role();
        BeanUtils.copyProperties(po, role);
        roleService.updateById(role);
        return R.ok();
    }

    @Verify(key = "bnt.sysRole.remove")
    @DeleteMapping("/removeById/{id}")
    public R removeById(@PathVariable Long id) {
        if (id < 1) {
            return R.fail().message("参数错误");
        }
        return roleService.removeById(id) ?
                R.ok() : R.fail().message(id + "出现了错误");
    }

    @Verify(key = "bnt.sysRole.remove")
    @DeleteMapping("/removeByIds")
    public R removeByIds(@RequestBody List<Long> ids) {
        if (ids == null && ids.size() < 1) {
            return R.fail().message("参数错误");
        }
        roleService.removeByIds(ids);
        return R.ok();
    }

    // 查询角色的菜单
    @Verify(key = "bnt.sysRole.list")
    @GetMapping("/getByRoleId/{roleId}")
    public R getByRoleId(@PathVariable Long roleId) {
        if (roleId < 0) {
            return R.fail().message("参数错误");
        }
        return R.ok(menuService.getByRoleId(roleId));
    }

    // 为角色分配菜单
    @Verify(key = "bnt.sysRole.assignMenu")
    @PostMapping("/assignMenu")
    public R assignMenu(@RequestBody @Validated AssignMenuPo po) {
        menuService.assignMenu(po);
        return R.ok();
    }

}
